package com.xywy.start;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.FragmentMainActivity;
import com.xywy.common.constants.ResultCode;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.common.util.MLog;
import com.xywy.common.widget.MyProgress;
import com.xywy.common.widget.TitleBar2;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 会员登陆 - 切换账户成寻医问药账户， 此类暂时废弃
 * 
 * @author xiaogd
 * 
 */
public class LoginSwitch1Activity extends BaseActivity {
	private EditText username;
	private EditText pwd;
	private ImageView mCancel;
	private ImageView pCancel;
	private Context mContext;
	private Button button;
	private TextView tvGo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.start_login_switch);

		this.preInit();
		this.initView();
		this.loadData();
		this.setListener();
	}

	public void startOnClick(View view) {
		switch (view.getId()) {

		}
		;
	}

	/**
	 * 进行验证,发送网络请求
	 */
	private void btnLogin() {
		boolean flag = false;
		if (username.getText().toString() == null
				|| username.getText().toString().length() == 0) {
			Toast.makeText(mContext, getString(R.string.me_mobile_not_null),
					Toast.LENGTH_LONG).show();
			flag = true;
			return;
		} else if (pwd.getText().toString() == null
				|| pwd.getText().toString().length() == 0) {
			Toast.makeText(mContext, getString(R.string.me_pwd_not_null),
					Toast.LENGTH_LONG).show();
			flag = true;
			return;
		}
		if (!flag) { // 提交数据
			this.sendMsg();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	public void preInit() {
		mContext = this.context;
	}

	@Override
	public void loadData() {

	}

	@Override
	public void initView() {
		// title
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.start_login_switch_title_bar);
		TextView tvTitle = titleBar.getTitle();
		tvGo = titleBar.getGo();
		titleBar.onClickBack(this);
		tvTitle.setText(getString(R.string.tv_start_login_switch));
		tvGo.setVisibility(View.INVISIBLE);
		mCancel = (ImageView) findViewById(R.id.iv_start_login_switch_mcancel);
		pCancel = (ImageView) findViewById(R.id.iv_start_login_switch_pcancel);

		username = (EditText) findViewById(R.id.et_login_switch_user);
		pwd = (EditText) findViewById(R.id.et_login_switch_pwd);

		button = (Button) findViewById(R.id.bt_start_login_switch);

	}

	MyProgress myProgress;
	JSONObject msgBack = null;
	/**
	 * 返回
	 */
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			MLog.i(TAG, "back:" + msg.obj + "resuletCode:" + msg.arg1);
			myProgress.stop();
			if (null != msg.obj) {
				try {
					msgBack = new JSONObject(msg.obj.toString());
				} catch (JSONException e) {
					e.printStackTrace();
				}
				if (msgBack.optInt("state") == ResultCode.SUCCESS) {
					String userid = msgBack.optString("userid");
					String mobilestr = msgBack.optString("phonenum");

					MLog.i(TAG, "当前人的userid:" + userid + "绑定的手机号，没有则为0:"
							+ mobilestr);

					// 将 userid 存入本地
					SharedPreferences sp = context.getSharedPreferences(
							"config", Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = sp.edit();
					editor.putString("userid", userid);
					editor.putString("mobile", mobilestr);

					editor.commit();

					/*
					 * Intent intent =new Intent(mContext,
					 * FragmentMainActivity.class);
					 * mContext.startActivity(intent); finish();
					 */

					Intent intent = new Intent(mContext,
							LoginSwitch2Activity.class);
					mContext.startActivity(intent);
				} else {
					Toast.makeText(mContext, msgBack.optString("error"),
							Toast.LENGTH_LONG).show();
					return;
				}
			}
		}
	};

	// 封装Parser参数
	private Parser paramsParser;
	// 封装httpclient参数
	private HashMap<String, Object> params;
	// 异步加载
	private AsyncHttpClient asyncHttpClient;

	// 处理网络请求
	public void sendMsg() {
		myProgress = new MyProgress(this, 10);
		myProgress.start();
		paramsParser = new Parser();

		// 封装参数
		params = new HashMap<String, Object>();

		// 请求路径
		paramsParser.action = getString(R.string.login_xywy);
		// 请求方式
		paramsParser.request = "post";

		// 请求参数
		params.put("xywyusername", username.getText().toString());
		params.put("xywypassowrd", pwd.getText().toString());

		paramsParser.map = params;
		// 传递上下文
		paramsParser.context = this.context;

		asyncHttpClient = AsyncHttpClient.getInstance();
		asyncHttpClient.exeute(paramsParser, onResultListenerAdapter);
	}

	OnResultListenerAdapter onResultListenerAdapter = new OnResultListenerAdapter() {
		@Override
		public <T extends Parser> void onSuccess(T resultParser, int resultCode) {
			super.onSuccess(resultParser, resultCode);
			Message message = Message.obtain();
			message.arg1 = resultCode;
			message.obj = resultParser.back;
			handler.sendMessage(message);
		}
	};

	private void setListener() {
		username.addTextChangedListener(mTextWatcher);
		pwd.addTextChangedListener(pTextWatcher);
	}

	boolean mFlag = false;
	TextWatcher mTextWatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence charSequence, int i, int i2,
				int i3) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void afterTextChanged(Editable editable) {
			if (editable.length() > 0) {
				mCancel.setVisibility(View.VISIBLE);
				mFlag = true;
				if (nFlag) {
					button.setBackgroundDrawable(mContext.getResources()
							.getDrawable(R.drawable.shape_button));
					button.setClickable(true);
				}
			} else {
				mCancel.setVisibility(View.INVISIBLE);
				mFlag = false;
				button.setBackgroundDrawable(mContext.getResources()
						.getDrawable(R.drawable.shape_button_bgwhite));
				button.setClickable(false);
			}
		}
	};

	boolean nFlag = false;
	TextWatcher pTextWatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence charSequence, int i, int i2,
				int i3) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void afterTextChanged(Editable editable) {
			if (editable.length() > 0) {
				pCancel.setVisibility(View.VISIBLE);
				nFlag = true;
				if (mFlag) {
					button.setBackgroundDrawable(mContext.getResources()
							.getDrawable(R.drawable.shape_button));
					button.setClickable(true);
				}
			} else {
				pCancel.setVisibility(View.INVISIBLE);
				nFlag = false;
				button.setBackgroundDrawable(mContext.getResources()
						.getDrawable(R.drawable.shape_button_bgwhite));
				button.setClickable(false);
			}
		}
	};

	public void loginOnClick(View view) {
		switch (view.getId()) {
		case R.id.iv_start_login_switch_mcancel:
			username.setText("");
			break;
		case R.id.iv_start_login_switch_pcancel:
			pwd.setText("");
			break;
		case R.id.bt_start_login_switch:
			btnLogin();
			break;
		}
	}
}
